package com.wwwarehouse.contract.program_operation.resources_factor_page;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.contract.adapter.RescourceFactorAdapter;
import com.wwwarehouse.contract.bean.ChooseModuleBean;
import com.wwwarehouse.contract.core.ContractConstant;
import com.wwwarehouse.contract.event.CheckAllResFactorEvent;
import com.wwwarehouse.contract.event.CheckHaveResEvent;
import com.wwwarehouse.contract.event.CheckNeedResEvent;
import com.wwwarehouse.contract.program_operation.CustomDisGridView;
import com.wwwarehouse.contract.request.ModuleQueryRequest;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import contract.wwwarehouse.com.contract.R;
import de.greenrobot.event.EventBus;

/**
 * Created by feng.gao on 2017/9/22.
 * 资源要素主页
 */


public class ResourceFactorMainFragment extends BaseFragment implements View.OnClickListener {
	private ImageView mHaveAddIv, mNeedAddIv; //两个加号按钮
	private TextView mHaveFixTv, mNeedFixTv; //上下两个文字按钮
	private CustomDisGridView mHaveGv, mNeedGv;  //上下两个gridView

	private RelativeLayout mHaveRl; //控制拥有资源显示的布局
	private RelativeLayout mNeedRl; //控制需要资源的布局

	private List<ChooseModuleBean.ModuleList.BeanList> mNeedResourceFactorCheckedList = new ArrayList<>(); //需要的资源要素已选的条目
	private List<ChooseModuleBean.ModuleList.BeanList> mDefaultNeedResourceFactorCheckedList = new ArrayList<>(); //需要的资源要素已选的条目

	private List<ChooseModuleBean.ModuleList.BeanList> mHaveResourceFactorCheckedList = new ArrayList<>(); //拥有的资源要素已选的条目

	private ModuleQueryRequest mHaveResourceParams;
	private ModuleQueryRequest mNeedResourceParams;

	private Button mConfirmBtn;
	private boolean isFunction;

	@Nullable
	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		return inflater.inflate(R.layout.resource_factor_fragment, container, false);
	}

	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		EventBus.getDefault().register(this);
		mHaveAddIv = (ImageView) view.findViewById(R.id.have_add_iv);
		mNeedAddIv = (ImageView) view.findViewById(R.id.need_add_iv);
		mHaveFixTv = (TextView) view.findViewById(R.id.fix_tv_top_tv);
		mNeedFixTv = (TextView) view.findViewById(R.id.need_tv_bottom_tv);
		mHaveGv = (CustomDisGridView) view.findViewById(R.id.have_gv);
		mNeedGv = (CustomDisGridView) view.findViewById(R.id.need_gv);
		mHaveRl = (RelativeLayout) view.findViewById(R.id.have_gv_layout);
		mNeedRl = (RelativeLayout) view.findViewById(R.id.need_gv_layout);
		mConfirmBtn = (Button) view.findViewById(R.id.confirm_btn);

		mHaveFixTv.setOnClickListener(this);
		mNeedFixTv.setOnClickListener(this);
		mHaveAddIv.setOnClickListener(this);
		mNeedAddIv.setOnClickListener(this);
		mConfirmBtn.setOnClickListener(this);

		isFunction = getArguments().getBoolean(ContractConstant.KEY_IS_FUNCTION);

		if (getArguments().getSerializable(ContractConstant.KEY_BUNDLE_HAVE_RESOURCE_CHECKED_DATA) != null) {
			mHaveResourceFactorCheckedList = (List<ChooseModuleBean.ModuleList.BeanList>) getArguments().getSerializable(ContractConstant.KEY_BUNDLE_HAVE_RESOURCE_CHECKED_DATA);
			if (mHaveResourceFactorCheckedList.size() > 0) {
				mHaveRl.setVisibility(View.VISIBLE);
				mHaveAddIv.setVisibility(View.GONE);
				RescourceFactorAdapter mNeedAdapter = new RescourceFactorAdapter(mHaveResourceFactorCheckedList, mActivity);
				mHaveGv.setAdapter(mNeedAdapter);
			}
		}
		if (getArguments().getSerializable(ContractConstant.KEY_BUNDLE_NEED_RESOURCE_CHECKED_DATA) != null) {
			mDefaultNeedResourceFactorCheckedList = (List<ChooseModuleBean.ModuleList.BeanList>) getArguments().getSerializable(ContractConstant.KEY_BUNDLE_NEED_RESOURCE_CHECKED_DATA);
			if (mDefaultNeedResourceFactorCheckedList.size() > 0) {
				mNeedRl.setVisibility(View.VISIBLE);
				mNeedAddIv.setVisibility(View.GONE);
				RescourceFactorAdapter mNeedAdapter = new RescourceFactorAdapter(mDefaultNeedResourceFactorCheckedList, mActivity);
				mNeedGv.setAdapter(mNeedAdapter);
			}
		}
		checkBtnClickable();
		if (getArguments() != null && getArguments().getSerializable(ContractConstant.KEY_BUNDLE_HAVE_RESOURCE_PARAMS) != null) {
			mHaveResourceParams = (ModuleQueryRequest) getArguments().getSerializable(ContractConstant.KEY_BUNDLE_HAVE_RESOURCE_PARAMS);
		}
		if (getArguments() != null && getArguments().getSerializable(ContractConstant.KEY_BUNDLE_NEED_RESOURCE_PARAMS) != null) {
			mNeedResourceParams = (ModuleQueryRequest) getArguments().getSerializable(ContractConstant.KEY_BUNDLE_NEED_RESOURCE_PARAMS);
		}
	}

	private void checkBtnClickable() {
		if (mHaveResourceFactorCheckedList.size() > 0 && mDefaultNeedResourceFactorCheckedList.size() > 0) {
			mConfirmBtn.setEnabled(true);
			mConfirmBtn.setBackground(mActivity.getResources().getDrawable(R.drawable.common_btn1_normal_shape));
		} else {
			mConfirmBtn.setEnabled(false);
			mConfirmBtn.setBackground(mActivity.getResources().getDrawable(R.drawable.common_btn1_noclick_shape));
		}
	}

	@Override
	public void setTitle() {
		if (peekFragment() instanceof ResourceFactorMainFragment) {
			mActivity.setTitle(R.string.choose_resources_title);
		}
	}

	@Override
	public void requestDatas() {

	}

	@Override
	public void onClick(View v) {
		int clickId = v.getId();
		if (clickId == R.id.have_add_iv || clickId == R.id.fix_tv_top_tv) {
			//拥有的资源要素
			Bundle bundle = new Bundle();
			if (mHaveResourceFactorCheckedList.size() > 0) {
				bundle.putSerializable(ContractConstant.KEY_BUNDLE_HAVE_RESOURCE_CHECKED_DATA, (Serializable) mHaveResourceFactorCheckedList);
			}
			if (mHaveResourceParams != null) {
				bundle.putSerializable(ContractConstant.KEY_BUNDLE_HAVE_RESOURCE_PARAMS, mHaveResourceParams);
			}
			bundle.putBoolean(ContractConstant.KEY_IS_FUNCTION , isFunction);
			HaveResourceFactorViewPagerFragment haveResourceFactorViewPagerFragment = new HaveResourceFactorViewPagerFragment();
			haveResourceFactorViewPagerFragment.setArguments(bundle);
			pushFragment(haveResourceFactorViewPagerFragment);
		} else if (clickId == R.id.need_add_iv || clickId == R.id.need_tv_bottom_tv) {
			//需要的资源要素
			//拥有的资源要素
			Bundle bundle = new Bundle();
			if (mNeedResourceFactorCheckedList.size() > 0) {
				bundle.putSerializable(ContractConstant.KEY_BUNDLE_NEED_RESOURCE_CHECKED_DATA, (Serializable) mNeedResourceFactorCheckedList);
			} else if (mDefaultNeedResourceFactorCheckedList.size() > 0) {
				bundle.putSerializable(ContractConstant.KEY_BUNDLE_NEED_RESOURCE_CHECKED_DATA, (Serializable) mDefaultNeedResourceFactorCheckedList);
			}
			if (mNeedResourceParams != null) {
				bundle.putSerializable(ContractConstant.KEY_BUNDLE_NEED_RESOURCE_PARAMS, mNeedResourceParams);
			}
			bundle.putBoolean(ContractConstant.KEY_IS_FUNCTION , isFunction);
			NeedResourceFactorViewPagerFragment needResourceFactorViewPagerFragment = new NeedResourceFactorViewPagerFragment();
			needResourceFactorViewPagerFragment.setArguments(bundle);
			pushFragment(needResourceFactorViewPagerFragment);

		} else if (clickId == R.id.confirm_btn) {
			CheckAllResFactorEvent allResFactorEvent = new CheckAllResFactorEvent();
			allResFactorEvent.setmHaveResFactorCheckedList(mHaveResourceFactorCheckedList);
			if (mNeedResourceFactorCheckedList.size() > 0) {
				allResFactorEvent.setmNeedResFactorCheckedList(mNeedResourceFactorCheckedList);
			} else if (mDefaultNeedResourceFactorCheckedList.size() > 0) {
				allResFactorEvent.setmNeedResFactorCheckedList(mDefaultNeedResourceFactorCheckedList);
			}
			EventBus.getDefault().post(allResFactorEvent);
			popFragment();
		}
	}

	/**
	 * 点击拥有的资源要素返回的数据
	 *
	 * @param checkIndusEvent
	 */
	public void onEventMainThread(CheckHaveResEvent checkIndusEvent) {
		mHaveResourceFactorCheckedList = checkIndusEvent.getIndustryChecks();
		if (mHaveResourceFactorCheckedList.size() > 0) {
			mHaveRl.setVisibility(View.VISIBLE);
			mHaveAddIv.setVisibility(View.GONE);
			RescourceFactorAdapter mNeedAdapter = new RescourceFactorAdapter(mHaveResourceFactorCheckedList, mActivity);
			mHaveGv.setAdapter(mNeedAdapter);
		}
		checkBtnClickable();

	}

	/**
	 * 点击需要的资源要素返回的数据
	 *
	 * @param checkIndusEvent
	 */
	public void onEventMainThread(CheckNeedResEvent checkIndusEvent) {
		mNeedResourceFactorCheckedList = checkIndusEvent.getIndustryChecks();
		if (mNeedResourceFactorCheckedList.size() > 0) {
			mDefaultNeedResourceFactorCheckedList = mNeedResourceFactorCheckedList;
			mNeedRl.setVisibility(View.VISIBLE);
			mNeedAddIv.setVisibility(View.GONE);
			RescourceFactorAdapter mNeedAdapter = new RescourceFactorAdapter(mNeedResourceFactorCheckedList, mActivity);
			mNeedGv.setAdapter(mNeedAdapter);
		}
		checkBtnClickable();

	}



	@Override
	public void onDestroy() {
		super.onDestroy();
		EventBus.getDefault().unregister(this);
	}
}
